﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CoreData;
using System.Reflection;

namespace SchoolManagementSystem.Forms.BusinessForm
{
    public partial class frmSelectBranch : DevExpress.XtraEditors.XtraForm
    {
        public frmSelectBranch()
        {
            InitializeComponent();
            LoadData();
        }
        public long ID { get; set; }
        public string Code { get; set; }
        public string Name { get; set; }

        void LoadData()
        {
            var model = RegisterService.RegisterServices.BranchService;
            var data = (List<Branch>)model.GetList();

            if (data == null)
            {
                data = new List<Branch>();
            }
          

            gridControl1.DataSource = data;
            gridControl1.RefreshDataSource();
            gridView1.ViewCaption = "Danh sách có " + gridView1.RowCount + " dòng";

            PropertyInfo[] propertyInfos;
            propertyInfos = new Branch().GetType().GetProperties(); ;

            foreach (PropertyInfo proInfo in propertyInfos)
            {
                AttributeCollection attributes = TypeDescriptor.GetProperties(new Branch().GetType())[proInfo.Name].Attributes;

                DescriptionAttribute myAttribute =
                   (DescriptionAttribute)attributes[typeof(DescriptionAttribute)];
                if (String.IsNullOrEmpty(myAttribute.Description))
                {
                    gridView1.Columns[proInfo.Name].Visible = false;
                }
                else
                {
                    gridView1.Columns[proInfo.Name].Caption = myAttribute.Description;
                    gridView1.Columns[proInfo.Name].OptionsColumn.AllowEdit = false;
                }
            }
            gridView1.Columns["SelectRowExt"].Visible = false;
           // gridView1.Columns["BranchExt"].GroupIndex = 0;
           // gridView1.Columns["TypeExt"].GroupIndex = 1;
            gridView1.ExpandAllGroups();

        }

        int FocusedRow
        {
            get
            {
                return gridView1.FocusedRowHandle;
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (gridView1.RowCount != 0)
            {
                ID = long.Parse(gridView1.GetRowCellValue(FocusedRow, "ID").ToString());
                Name = (gridView1.GetRowCellValue(FocusedRow, "Name").ToString());
                Code = (gridView1.GetRowCellValue(FocusedRow, "Code").ToString());
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        
    }
}
